# coding: utf-8
# Author: Legend奇
# @Time： 2020/7/20 下午3:18

from gettext import gettext as _

from tortoise import fields

from base.base_model import MyAbstractBaseModel
from base.choices import PROTOCOL, METHOD, PERMISSION_TYPES


class Api(MyAbstractBaseModel):
    regex = fields.CharField(max_length=255, default='', verbose_name=_('api regex'))
    protocol = fields.IntField(default=0, choices=PROTOCOL, verbose_name=_('api protocol'))
    method = fields.IntField(default=0, choices=METHOD, null=True, verbose_name=_('api http request method'))
    name = fields.CharField(max_length=255, default='', verbose_name=_('api name'))
    position = fields.CharField(max_length=128, default='', verbose_name=_('api position'))
    group = fields.CharField(max_length=128, default='', verbose_name=_('api group'))
    code = fields.CharField(max_length=32, default='', verbose_name=_('api code'))
    user_default = fields.BooleanField(default=0, verbose_name=_('is user default'))

    class Meta:
        table = 'api'
        table_description = "api info"


class PermissionGroup(MyAbstractBaseModel):
    description = fields.CharField(max_length=128, default='', null=True)
    name = fields.CharField(max_length=50, default='', )
    type = fields.SmallIntField(default=0, choices=PERMISSION_TYPES)
    auth_time = fields.IntField(default=3600)  # 单次认证的有效时间
    menu = fields.ManyToManyField('models.Menu', through='permissiongroup_menu')
    api = fields.ManyToManyField('models.Api', through='permissiongroup_api')

    class Meta:
        table = 'permissiongroup'
        verbose_name = _('permission group')


class User(MyAbstractBaseModel):
    username = fields.CharField(max_length=50, default='', verbose_name=_('user account'))
    password = fields.CharField(max_length=255, default='', verbose_name=_('user pass code'))
    alias = fields.CharField(max_length=128, default='', verbose_name=_('user alias'))
    cellphone_number = fields.BigIntField(null=True, verbose_name=_('user cellphone number'))
    email = fields.CharField(default='', max_length=50, verbose_name=_('user email'))
    is_active = fields.BooleanField(default=1, verbose_name=_('user is active or not'))
    create_timestamp = fields.BigIntField(default=0, verbose_name=_('create time'))
    creator = fields.IntField(null=True, verbose_name=_('creator'))
    is_super = fields.BooleanField(default=0)
    permission_group = fields.ForeignKeyField('models.PermissionGroup', null=True, on_delete=fields.SET_NULL)

    _is_authenticated = False

    @property
    def is_authenticated(self):
        return self._is_authenticated

    def authenticate(self):
        self._is_authenticated = True

    class Meta:
        table = "user"
        verbose_name = _('user')


class Menu(MyAbstractBaseModel):
    name = fields.CharField(max_length=255, default='', verbose_name=_('menu name'))
    expandable = fields.BooleanField(default=0, verbose_name=_('expandable'))
    bold = fields.BooleanField(default=0, verbose_name=_('bold'))
    css_icon = fields.CharField(max_length=255, default='', verbose_name=_('css icon'))
    link = fields.CharField(max_length=255, default='', verbose_name=_('link'))
    father = fields.ForeignKeyField("models.Menu", related_name='father_set', on_delete=fields.CASCADE, null=True,
                                    verbose_name=_('super menu id'))
    supers = fields.ManyToManyField("models.Menu", through='menu_supers')
    apis = fields.ManyToManyField('models.Api', through='menu_apis')

    class Meta:
        table = 'menu'
        verbose_name = _('menu')
